<?php
/**
* @file $Id: Calendar.php 597 2007-07-12 22:30:12Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

if(!$_REQUEST['month'])
	$_REQUEST['month'] = date("n");
else
	$_REQUEST['month'] = MonthNWSwitch($_REQUEST['month'],'tonum')*1;
if(!$_REQUEST['year'])
	$_REQUEST['year'] = date("Y");

$time = mktime(0,0,0,$_REQUEST['month'],1,$_REQUEST['year']);
DrawHeader(ProgramTitle());

if(!$_REQUEST['calendar_id'])
{
	$default_RET = DBGet(DBQuery("SELECT CALENDAR_ID FROM ATTENDANCE_CALENDARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND DEFAULT_CALENDAR='Y'"));
	if(count($default_RET))
		$_REQUEST['calendar_id'] = $default_RET[1]['CALENDAR_ID'];
	else
	{
		$calendars_RET = DBGet(DBQuery("SELECT CALENDAR_ID FROM ATTENDANCE_CALENDARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
		if(count($calendars_RET))
			$_REQUEST['calendar_id'] = $calendars_RET[1]['CALENDAR_ID'];
		else
			$error = array(_('There are no calendars yet setup.'));
	}
}
unset($_SESSION['_REQUEST_vars']['calendar_id']);

if($_REQUEST['modfunc']=='create')
{
	$message = '<TABLE><TR><TD colspan=7 align=center>'._('Title').'<INPUT type=text name=title> <INPUT type=checkbox name=default value=Y> '._('Default Calendar for this School').'<BR><BR></TD></TR><TR><TD colspan=7 align=center>'._('From').' '.PrepareDate('','_min').' '._('To').' '.PrepareDate('','_max').'</TD></TR><TR><TD><INPUT type=checkbox value=Y name=weekdays[0]>'._('Sunday').'</TD><TD><INPUT type=checkbox value=Y name=weekdays[1] CHECKED>'._('Monday').'</TD><TD><INPUT type=checkbox value=Y name=weekdays[2] CHECKED>'._('Tuesday').'</TD><TD><INPUT type=checkbox value=Y name=weekdays[3] CHECKED>'._('Wednesday').'</TD><TD><INPUT type=checkbox value=Y name=weekdays[4] CHECKED>'._('Thursday').'</TD><TD><INPUT type=checkbox value=Y name=weekdays[5] CHECKED>'._('Friday').'</TD><TD><INPUT type=checkbox value=Y name=weekdays[6]>'._('Saturday').'</TD></TR></TABLE>';
	if(Prompt(_('Create a new calendar'),'',$message))
	{
		$begin = mktime(0,0,0,MonthNWSwitch($_REQUEST['month_min'],'to_num'),$_REQUEST['day_min']*1,$_REQUEST['year_min']) + 43200;
		$end = mktime(0,0,0,MonthNWSwitch($_REQUEST['month_max'],'to_num'),$_REQUEST['day_max']*1,$_REQUEST['year_max']) + 43200;

		$weekday = date('w',$begin);

		$calendar_id = DBGet(DBQuery("SELECT ".db_seq_nextval('CALENDARS_SEQ')." AS CALENDAR_ID ".FROM_DUAL));
		$calendar_id = $calendar_id[1]['CALENDAR_ID'];

		$existing_RET = DBGet(DBQuery("SELECT COUNT(*) AS COUNT FROM ATTENDANCE_CALENDARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));

		DBQuery("INSERT INTO ATTENDANCE_CALENDARS (CALENDAR_ID,SYEAR,SCHOOL_ID,TITLE) values('".$calendar_id."','".UserSyear()."','".UserSchool()."','".$_REQUEST['title']."')");
		if($existing_RET[1]['COUNT']=='0')
		{
			DBQuery("UPDATE COURSE_PERIODS SET CALENDAR_ID='".$calendar_id."' WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			DBQuery("UPDATE STUDENT_ENROLLMENT SET CALENDAR_ID='".$calendar_id."' WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
		}

		if($begin>43200 && $end>43200)
		{
			for($i=$begin;$i<=$end;$i+=86400)
			{
				if($_REQUEST['weekdays'][$weekday]=='Y')
					DBQuery("INSERT INTO ATTENDANCE_CALENDAR (SYEAR,SCHOOL_ID,SCHOOL_DATE,MINUTES,CALENDAR_ID) values('".UserSyear()."','".UserSchool()."','".date('d-M-y',$i)."','999','".$calendar_id."')");
				$weekday++;
				if($weekday==7)
					$weekday = 0;
			}
		}

		$_REQUEST['calendar_id'] = $calendar_id;
		unset($error); // since the error is "there is no calendar"
		
		unset($_REQUEST['modfunc']);
		unset($_SESSION['_REQUEST_vars']['modfunc']);
		unset($_SESSION['_REQUEST_vars']['weekdays']);
		unset($_SESSION['_REQUEST_vars']['title']);
	}
}

if($_REQUEST['modfunc']=='populate')
{
	$message = '<TABLE><TR><TD colspan=7 align=center>'._('From').' '.PrepareDate('','_min').' '._('To').' '.PrepareDate('','_max').'</TD></TR><TR><TD><INPUT type=checkbox value=Y name=weekdays[0]>'._('Sunday').'</TD><TD><INPUT type=checkbox value=Y name=weekdays[1] CHECKED>'._('Monday').'</TD><TD><INPUT type=checkbox value=Y name=weekdays[2] CHECKED>'._('Tuesday').'</TD><TD><INPUT type=checkbox value=Y name=weekdays[3] CHECKED>'._('Wednesday').'</TD><TD><INPUT type=checkbox value=Y name=weekdays[4] CHECKED>'._('Thursday').'</TD><TD><INPUT type=checkbox value=Y name=weekdays[5] CHECKED>'._('Friday').'</TD><TD><INPUT type=checkbox value=Y name=weekdays[6]>'._('Saturday').'</TD></TR></TABLE>';
	if(Prompt(_('Populate this calendar'),'',$message))
	{
		$begin = mktime(0,0,0,MonthNWSwitch($_REQUEST['month_min'],'to_num'),$_REQUEST['day_min']*1,$_REQUEST['year_min']) + 43200;
		$end = mktime(0,0,0,MonthNWSwitch($_REQUEST['month_max'],'to_num'),$_REQUEST['day_max']*1,$_REQUEST['year_max']) + 43200;

		$weekday = date('w',$begin);

		if($begin>43200 && $end>43200)
		{
			for($i=$begin;$i<=$end;$i+=86400)
			{
				if($_REQUEST['weekdays'][$weekday]=='Y')
					DBQuery("INSERT INTO ATTENDANCE_CALENDAR (SYEAR,SCHOOL_ID,SCHOOL_DATE,MINUTES,CALENDAR_ID) values('".UserSyear()."','".UserSchool()."','".date('d-M-y',$i)."','999','".$_REQUEST['calendar_id']."')");
				$weekday++;
				if($weekday==7)
					$weekday = 0;
			}
		}
		
		unset($_REQUEST['modfunc']);
		unset($_SESSION['_REQUEST_vars']['modfunc']);
		unset($_SESSION['_REQUEST_vars']['weekdays']);
		unset($_SESSION['_REQUEST_vars']['title']);
	}
}


if($_REQUEST['modfunc']=='delete_calendar')
{
	if(DeletePrompt(_('calendar')))
	{
		DBQuery("DELETE FROM ATTENDANCE_CALENDAR WHERE CALENDAR_ID='$_REQUEST[calendar_id]'");
		DBQuery("DELETE FROM ATTENDANCE_CALENDARS WHERE CALENDAR_ID='$_REQUEST[calendar_id]'");
		$default_RET = DBGet(DBQuery("SELECT CALENDAR_ID FROM ATTENDANCE_CALENDARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND DEFAULT_CALENDAR='Y'"));
		if(count($default_RET))
			$_REQUEST['calendar_id'] = $default_RET[1]['CALENDAR_ID'];
		else
		{
			$calendars_RET = DBGet(DBQuery("SELECT CALENDAR_ID FROM ATTENDANCE_CALENDARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
			if(count($calendars_RET))
				$_REQUEST['calendar_id'] = $calendars_RET[1]['CALENDAR_ID'];
			else
				$error = array(_('There are no calendars yet setup.'));
		}
		unset($_REQUEST['modfunc']);
		unset($_SESSION['_REQUEST_vars']['modfunc']);
	}
}

if($_REQUEST['modfunc']=='detail')
{
	if($_REQUEST['month_values'] && $_REQUEST['day_values'] && $_REQUEST['year_values'])
	{
		$_REQUEST['values']['SCHOOL_DATE'] = $_REQUEST['day_values']['SCHOOL_DATE'].'-'.$_REQUEST['month_values']['SCHOOL_DATE'].'-'.$_REQUEST['year_values']['SCHOOL_DATE'];
		if(!VerifyDate($_REQUEST['values']['SCHOOL_DATE']))
			unset($_REQUEST['values']['SCHOOL_DATE']);
	}

	if($_POST['button']==_('Save') && AllowEdit())
	{
		if($_REQUEST['values'])
		{
			if($_REQUEST['event_id']!='new')
			{
				$sql = "UPDATE CALENDAR_EVENTS SET ";

				foreach($_REQUEST['values'] as $column=>$value)
					$sql .= $column."='".str_replace("\'","''",$value)."',";

				$sql = substr($sql,0,-1) . " WHERE ID='$_REQUEST[event_id]'";
				DBQuery($sql);
			}
			else
			{
				$sql = "INSERT INTO CALENDAR_EVENTS ";

				$fields = 'ID,SYEAR,SCHOOL_ID,';
				$values = db_seq_nextval('CALENDAR_EVENTS_SEQ').",'".UserSyear()."','".UserSchool()."',";

				$go = 0;
				foreach($_REQUEST['values'] as $column=>$value)
				{
					if($value)
					{
						$fields .= $column.',';
						$values .= "'".str_replace("\'","''",$value)."',";
						$go = true;
					}
				}
				$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';

				if($go)
					DBQuery($sql);
			}
			echo '<SCRIPT language=javascript>opener.document.location = "Modules.php?modname='.$_REQUEST['modname'].'&year='.$_REQUEST['year'].'&month='.MonthNWSwitch($_REQUEST['month'],'tochar').'"; window.close();</script>';
			unset($_REQUEST['values']);
			unset($_SESSION['_REQUEST_vars']['values']);
		}
	}
	elseif($_REQUEST['button']==_('Delete'))
	{
		if(DeletePrompt(_('event')))
		{
			DBQuery("DELETE FROM CALENDAR_EVENTS WHERE ID='$_REQUEST[event_id]'");
			echo '<SCRIPT language=javascript>opener.document.location = "Modules.php?modname='.$_REQUEST['modname'].'&year='.$_REQUEST['year'].'&month='.MonthNWSwitch($_REQUEST['month'],'tochar').'"; window.close();</script>';
			unset($_REQUEST['values']);
			unset($_SESSION['_REQUEST_vars']['values']);
			unset($_REQUEST['button']);
			unset($_SESSION['_REQUEST_vars']['button']);
		}
	}
	else
	{
		if($_REQUEST['event_id'])
		{
			if($_REQUEST['event_id']!='new')
			{
				$RET = DBGet(DBQuery("SELECT TITLE,DESCRIPTION,to_char(SCHOOL_DATE,'dd-MON-yy') AS SCHOOL_DATE FROM CALENDAR_EVENTS WHERE ID='$_REQUEST[event_id]'"));
				$title = $RET[1]['TITLE'];
			}
			else
			{
				$title = _('New Event');
				$RET[1]['SCHOOL_DATE'] = $_REQUEST['school_date'];
			}
			echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&modfunc=detail&event_id=$_REQUEST[event_id]&month=$_REQUEST[month]&year=$_REQUEST[year]&include_top=false METHOD=POST>";
		}
		else
		{
			$RET = DBGet(DBQuery("SELECT TITLE,STAFF_ID,to_char(DUE_DATE,'dd-MON-yy') AS SCHOOL_DATE,DESCRIPTION FROM GRADEBOOK_ASSIGNMENTS WHERE ASSIGNMENT_ID='$_REQUEST[assignment_id]'"));
			$title = $RET[1]['TITLE'];
			$RET[1]['STAFF_ID'] = GetTeacher($RET[1]['STAFF_ID']);
		}

		echo '<BR>';
		PopTable('header',$title);
		echo '<TABLE>';
		echo '<TR><TD>'._('Date').'</TD><TD>'.DateInput($RET[1]['SCHOOL_DATE'],'values[SCHOOL_DATE]','',false).'</TD></TR>';
		echo '<TR><TD>'._('Title').'</TD><TD>'.TextInput($RET[1]['TITLE'],'values[TITLE]').'</TD></TR>';
		if($RET[1]['STAFF_ID'])
			echo '<TR><TD>'._('Teacher').'</TD><TD>'.TextAreaInput($RET[1]['STAFF_ID'],'values[STAFF_ID]').'</TD></TR>';
		echo '<TR><TD>'._('Notes').'</TD><TD>'.TextAreaInput($RET[1]['DESCRIPTION'],'values[DESCRIPTION]').'</TD></TR>';
		if(AllowEdit())
		{
			echo '<TR><TD colspan=2 align=center><INPUT type=submit name=button value='._('Save').'>';
			if($_REQUEST['event_id']!='new')
				echo '<INPUT type=submit name=button value='._('Delete').'>';
			echo '</TD></TR>';
		}
		echo '</TABLE>';
		PopTable('footer');
		echo '</FORM>';

		unset($_REQUEST['values']);
		unset($_SESSION['_REQUEST_vars']['values']);
		unset($_REQUEST['button']);
		unset($_SESSION['_REQUEST_vars']['button']);
	}
}

if($_REQUEST['modfunc']=='list_events')
{
	if($_REQUEST['day_start'] && $_REQUEST['month_start'] && $_REQUEST['year_start'])
	{
		while(!VerifyDate($start_date = $_REQUEST['day_start'].'-'.$_REQUEST['month_start'].'-'.$_REQUEST['year_start']))
			$_REQUEST['day_start']--;
	}
	else
	{
		$min_date = DBGet(DBQuery("SELECT min(SCHOOL_DATE) AS MIN_DATE FROM ATTENDANCE_CALENDAR WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
		if($min_date[1]['MIN_DATE'])
			$start_date = $min_date[1]['MIN_DATE'];
		else
			$start_date = '01-'.strtoupper(date('M-y'));
	}

	if($_REQUEST['day_end'] && $_REQUEST['month_end'] && $_REQUEST['year_end'])
	{
		while(!VerifyDate($end_date = $_REQUEST['day_end'].'-'.$_REQUEST['month_end'].'-'.$_REQUEST['year_end']))
			$_REQUEST['day_end']--;
	}
	else
	{
		$max_date = DBGet(DBQuery("SELECT max(SCHOOL_DATE) AS MAX_DATE FROM ATTENDANCE_CALENDAR WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
		if($max_date[1]['MAX_DATE'])
			$end_date = $max_date[1]['MAX_DATE'];
		else
			$end_date = strtoupper(date('d-M-y'));
	}

	echo '<FORM action=Modules.php?modname='.$_REQUEST['modname'].'&modfunc='.$_REQUEST['modfunc'].'&month='.$_REQUEST['month'].'&year='.$_REQUEST['year'].' METHOD=POST>';
	DrawHeader(' &nbsp; &nbsp; <B>'._('Report Timeframe').': </B>'.PrepareDate($start_date,'_start').' - '.PrepareDate($end_date,'_end').' <A HREF=Modules.php?modname='.$_REQUEST['modname'].'&month='.$_REQUEST['month'].'&year='.$_REQUEST['year'].'>Back to Calendar</A>','<INPUT type=submit value=Go>');
	$functions = array('SCHOOL_DATE'=>'ProperDate');
	$events_RET = DBGet(DBQuery("SELECT ID,SCHOOL_DATE,TITLE,DESCRIPTION FROM CALENDAR_EVENTS WHERE SCHOOL_DATE BETWEEN '".$start_date."' AND '".$end_date."' AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"),$functions);
	ListOutput($events_RET,array('SCHOOL_DATE'=>_('Date'),'TITLE'=>_('Event'),'DESCRIPTION'=>_('Description')),_('Event'),_('Events'));
	echo '</FORM>';
}

if(!$_REQUEST['modfunc'])
{
	$last = 31;
	while(!checkdate($_REQUEST['month'], $last, $_REQUEST['year']))
		$last--;

	$calendar_RET = DBGet(DBQuery("SELECT to_char(SCHOOL_DATE,'dd-MON-YY') as SCHOOL_DATE,MINUTES,BLOCK FROM ATTENDANCE_CALENDAR WHERE SCHOOL_DATE BETWEEN '".date('d-M-y',$time)."' AND '".date('d-M-y',mktime(0,0,0,$_REQUEST['month'],$last,$_REQUEST['year']))."' AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND CALENDAR_ID='".$_REQUEST['calendar_id']."'"),array(),array('SCHOOL_DATE'));
	if($_REQUEST['minutes'])
	{
		foreach($_REQUEST['minutes'] as $date=>$minutes)
		{
			if($calendar_RET[$date])
			{
				if($minutes!='0' && $minutes!='')
					DBQuery("UPDATE ATTENDANCE_CALENDAR SET MINUTES='$minutes' WHERE SCHOOL_DATE='$date' AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND CALENDAR_ID='".$_REQUEST['calendar_id']."'");
				else
					DBQuery("DELETE FROM ATTENDANCE_CALENDAR WHERE SCHOOL_DATE='$date' AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND CALENDAR_ID='".$_REQUEST['calendar_id']."'");
			}
			elseif($minutes!='0' && $minutes!='')
				DBQuery("INSERT INTO ATTENDANCE_CALENDAR (SYEAR,SCHOOL_ID,SCHOOL_DATE,CALENDAR_ID,MINUTES) values('".UserSyear()."','".UserSchool()."','".$date."','".$_REQUEST['calendar_id']."','".$minutes."')");
		}
		$calendar_RET = DBGet(DBQuery("SELECT to_char(SCHOOL_DATE,'dd-MON-YY') as SCHOOL_DATE,MINUTES,BLOCK FROM ATTENDANCE_CALENDAR WHERE SCHOOL_DATE BETWEEN '".date('d-M-y',$time)."' AND '".date('d-M-y',mktime(0,0,0,$_REQUEST['month'],$last,$_REQUEST['year']))."' AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND CALENDAR_ID='".$_REQUEST['calendar_id']."'"),array(),array('SCHOOL_DATE'));
		unset($_REQUEST['minutes']);
		unset($_SESSION['_REQUEST_vars']['minutes']);
	}
	if($_REQUEST['all_day'])
	{
		foreach($_REQUEST['all_day'] as $date=>$yes)
		{
			if($yes=='Y')
			{
				if($calendar_RET[$date])
					DBQuery("UPDATE ATTENDANCE_CALENDAR SET MINUTES='999' WHERE SCHOOL_DATE='$date' AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND CALENDAR_ID='".$_REQUEST['calendar_id']."' AND CALENDAR_ID='".$_REQUEST['calendar_id']."'");
				else
					DBQuery("INSERT INTO ATTENDANCE_CALENDAR (SYEAR,SCHOOL_ID,SCHOOL_DATE,CALENDAR_ID,MINUTES) values('".UserSyear()."','".UserSchool()."','".$date."','".$_REQUEST['calendar_id']."','999')");
			}
			else
				DBQuery("DELETE FROM ATTENDANCE_CALENDAR WHERE SCHOOL_DATE='$date' AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND CALENDAR_ID='".$_REQUEST['calendar_id']."'");
		}
		$calendar_RET = DBGet(DBQuery("SELECT to_char(SCHOOL_DATE,'dd-MON-YY') as SCHOOL_DATE,MINUTES,BLOCK FROM ATTENDANCE_CALENDAR WHERE SCHOOL_DATE BETWEEN '".date('d-M-y',$time)."' AND '".date('d-M-y',mktime(0,0,0,$_REQUEST['month'],$last,$_REQUEST['year']))."' AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND CALENDAR_ID='".$_REQUEST['calendar_id']."'"),array(),array('SCHOOL_DATE'));
		unset($_REQUEST['all_day']);
		unset($_SESSION['_REQUEST_vars']['all_day']);
	}
	if($_REQUEST['blocks'])
	{
		foreach($_REQUEST['blocks'] as $date=>$block)
		{
			if($calendar_RET[$date])
			{
				DBQuery("UPDATE ATTENDANCE_CALENDAR SET BLOCK='".$block."' WHERE SCHOOL_DATE='$date' AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND CALENDAR_ID='".$_REQUEST['calendar_id']."'");
			}
		}
		$calendar_RET = DBGet(DBQuery("SELECT to_char(SCHOOL_DATE,'dd-MON-YY') as SCHOOL_DATE,MINUTES,BLOCK FROM ATTENDANCE_CALENDAR WHERE SCHOOL_DATE BETWEEN '".date('d-M-y',$time)."' AND '".date('d-M-y',mktime(0,0,0,$_REQUEST['month'],$last,$_REQUEST['year']))."' AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND CALENDAR_ID='".$_REQUEST['calendar_id']."'"),array(),array('SCHOOL_DATE'));
		unset($_REQUEST['blocks']);
		unset($_SESSION['_REQUEST_vars']['blocks']);
	}

	echo "<FORM action=Modules.php?modname=$_REQUEST[modname] METHOD=POST>";
	$link = '';
	$title_RET = DBGet(DBQuery("SELECT CALENDAR_ID,TITLE FROM ATTENDANCE_CALENDARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
	foreach($title_RET as $title)
	{
		$options[$title['CALENDAR_ID']] = $title['TITLE'];
	}
	if(AllowEdit())
	{
		$tmp_REQUEST = $_REQUEST;
		unset($tmp_REQUEST['calendar_id']);
		$link = SelectInput($_REQUEST['calendar_id'],'calendar_id','',$options,false," onchange='document.location.href=\"".PreparePHP_SELF($tmp_REQUEST).'&amp;calendar_id="+this.form.calendar_id.value;\' ',false)."<A HREF=Modules.php?modname=$_REQUEST[modname]&modfunc=create>".button('add')._("Create a new calendar")."</A> <A HREF=Modules.php?modname=$_REQUEST[modname]&modfunc=delete_calendar&calendar_id=$_REQUEST[calendar_id]>".button('remove').' '._('Delete this calendar')."</A><A HREF=Modules.php?modname=$_REQUEST[modname]&modfunc=populate&calendar_id=$_REQUEST[calendar_id]>".button('add').' '._('Populate this calendar').'</A>';
	}
	DrawHeader(' &nbsp; &nbsp; <B>'._('Calendar Month').': </B>'.PrepareDate(strtoupper(date("d-M-y",$time)),'',false,array('M'=>1,'Y'=>1,'submit'=>true)),' <A HREF=Modules.php?modname='.$_REQUEST['modname'].'&modfunc=list_events&month='.$_REQUEST['month'].'&year='.$_REQUEST['year'].'>'._('List Events').'</A> &nbsp; &nbsp;'.SubmitButton(_('Update')));
	DrawHeader($link);
	if(count($error))
		echo ErrorMessage($error,'fatal');
	echo '<BR>';

	$events_RET = DBGet(DBQuery("SELECT ID,to_char(SCHOOL_DATE,'dd-MON-yy') AS SCHOOL_DATE,TITLE FROM CALENDAR_EVENTS WHERE SCHOOL_DATE BETWEEN '".date('d-M-y',$time)."' AND '".date('d-M-y',mktime(0,0,0,$_REQUEST['month'],$last,$_REQUEST['year']))."' AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"),array(),array('SCHOOL_DATE'));
	if(User('PROFILE')=='parent' || User('PROFILE')=='student')
		$assignments_RET = DBGet(DBQuery("SELECT ASSIGNMENT_ID AS ID,to_char(a.DUE_DATE,'dd-MON-yy') AS SCHOOL_DATE,a.TITLE FROM GRADEBOOK_ASSIGNMENTS a,SCHEDULE s WHERE (a.COURSE_PERIOD_ID=s.COURSE_PERIOD_ID OR a.COURSE_ID=s.COURSE_ID) AND s.STUDENT_ID='".UserStudentID()."' AND (a.DUE_DATE BETWEEN s.START_DATE AND s.END_DATE OR s.END_DATE IS NULL) AND a.DUE_DATE BETWEEN '".date('d-M-y',$time)."' AND '".date('d-M-y',mktime(0,0,0,$_REQUEST['month'],$last,$_REQUEST['year']))."'"),array(),array('SCHOOL_DATE'));
	elseif(User('PROFILE')=='teacher')
		$assignments_RET = DBGet(DBQuery("SELECT ASSIGNMENT_ID AS ID,to_char(a.DUE_DATE,'dd-MON-yy') AS SCHOOL_DATE,a.TITLE FROM GRADEBOOK_ASSIGNMENTS a WHERE a.STAFF_ID='".User('STAFF_ID')."' AND a.DUE_DATE BETWEEN '".date('d-M-y',$time)."' AND '".date('d-M-y',mktime(0,0,0,$_REQUEST['month'],$last,$_REQUEST['year']))."'"),array(),array('SCHOOL_DATE'));

	$skip = date("w",$time);

	echo "<CENTER><TABLE bgcolor=#333366 border=1 bordercolor=black cellpadding=3><TR><TD>";
	echo "<TABLE border=0 bgcolor=#EEEEEE><TR bgcolor=black align=center>";
	echo "<TD><font color=white>"._('Sunday')."</font></TD><TD><font color=white>"._('Monday')."</font></TD><TD><font color=white>"._('Tuesday')."</font></TD><TD><font color=white>"._('Wednesday')."</font></TD><TD><font color=white>"._('Thursday')."</font></TD><TD><font color=white>"._('Friday')."</font></TD><TD width=99><font color=white>"._('Saturday')."</font></TD>";
	echo "</TR><TR>";

	if($skip)
	{
		echo "<td colspan=" . $skip . "></td>";
		$return_counter = $skip;
	}
	$blocks_RET = DBGet(DBQuery("SELECT DISTINCT BLOCK FROM SCHOOL_PERIODS WHERE BLOCK IS NOT NULL AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
	for($i=1;$i<=$last;$i++)
	{
		$day_time = mktime(0,0,0,$_REQUEST['month'],$i,$_REQUEST['year']);
		$date = strtoupper(date('d-M-y',$day_time));

		echo "<TD width=100 bgcolor=#FFFFFF valign=top><table width=100><tr><td width=5 valign=top>$i</td><td width=95 align=".ALIGN_RIGHT.">";
		if(AllowEdit())
		{
			if($calendar_RET[$date][1]['MINUTES']=='999')
				echo '<TABLE><TR><TD>'.CheckboxInput($calendar_RET[$date],"all_day[$date]",'','',false,'<IMG SRC=assets/check.gif height=10 vspace=0 hspace=0 border=0>').'</TD></TR></TABLE>';
			else
			{
				echo "<TABLE><TR><TD><INPUT type=checkbox name=all_day[$date] value=Y></TD>";
				echo '<TD>'.TextInput($calendar_RET[$date][1]['MINUTES'],"minutes[$date]",'','size=3').'</TD></TR></TABLE>';
			}
		}
		unset($options);
		if(count($blocks_RET)>1)
		{
			foreach($blocks_RET as $block)
				$options[$block['BLOCK']] = $block['BLOCK'];

			echo SelectInput($calendar_RET[$date][1]['BLOCK'],"blocks[$date]",'',$options);
		}
		echo "</td></tr><tr><TD colspan=2 height=50 valign=top>";

		if(count($events_RET[$date]))
		{
			echo '<TABLE cellpadding=0 cellspacing=0 border=0>';
			foreach($events_RET[$date] as $event)
				echo "<TR><TD>".button('dot','FF0000','',6)."</TD><TD><font size=-3><A HREF=# onclick='javascript:window.open(\"Modules.php?modname=$_REQUEST[modname]&modfunc=detail&event_id=$event[ID]&year=$_REQUEST[year]&month=".MonthNWSwitch($_REQUEST['month'],'tochar')."&include_top=false\",\"blank\",\"width=500,height=300\"); return false;'>".($event['TITLE']?$event['TITLE']:'***')."</A></font></TD></TR>";
			if(count($assignments_RET[$date]))
			{
				foreach($assignments_RET[$date] as $event)
					echo "<TR><TD>".button('dot','3333CC','',6)."</TD><TD><font size=-3><A HREF=# onclick='javascript:window.open(\"Modules.php?modname=$_REQUEST[modname]&modfunc=detail&assignment_id=$event[ID]&year=$_REQUEST[year]&month=".MonthNWSwitch($_REQUEST['month'],'tochar')."&include_top=false\",\"blank\",\"width=500,height=300\"); return false;'>".$event['TITLE']."</A></font></TD></TR>";
			}
			echo '</TABLE>';
		}
		elseif(count($assignments_RET[$date]))
		{
			echo '<TABLE cellpadding=0 cellspacing=0 border=0>';
			foreach($assignments_RET[$date] as $event)
				echo "<TR><TD>".button('dot','3333CC','',6)."</TD><TD><font size=-3><A HREF=# onclick='javascript:window.open(\"Modules.php?modname=$_REQUEST[modname]&modfunc=detail&assignment_id=$event[ID]&year=$_REQUEST[year]&month=".MonthNWSwitch($_REQUEST['month'],'tochar')."&include_top=false\",\"blank\",\"width=500,height=300\"); return false;'>".$event['TITLE']."</A></font></TD></TR>";
			echo '</TABLE>';
		}

		echo "</td></tr>";
		if(AllowEdit())
			echo "<tr><td valign=bottom align=".ALIGN_LEFT.">".button('add','',"# onclick='javascript:window.open(\"Modules.php?modname=$_REQUEST[modname]&modfunc=detail&event_id=new&school_date=$date&year=$_REQUEST[year]&month=".MonthNWSwitch($_REQUEST['month'],'tochar')."&include_top=false\",\"blank\",\"width=500,height=300\"); return false;'")."</td></tr>";
		echo "</table></TD>";
		$return_counter++;

		if($return_counter%7==0)
			echo "</TR><TR>";
	}
	echo "</TR></TABLE>";

	echo "</TD></TR></TABLE>";
	echo '<BR>'.SubmitButton(_('Update'));
	echo "</CENTER>";
	echo '</FORM>';
}
?>
